/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.conf;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.medusa.gruul.payment.api.enums.PayChannelEnum;
import com.medusa.gruul.payment.api.enums.WxPayEnum;
import com.medusa.gruul.payment.handler.WxPayMiniHandler;
import com.medusa.gruul.payment.router.PayMessageRouter;
import com.medusa.gruul.payment.service.impl.IpsPayServiceImpl;
import com.medusa.gruul.payment.service.impl.SftPayServiceImpl;
import com.medusa.gruul.payment.service.impl.SxfPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author whh
 */
@Component
public class PayConfig {

    @Autowired
    private WxPayMiniHandler wxPayMiniHandler;

    @Autowired
    private IpsPayServiceImpl ipsPayService;

    @Autowired
    private SxfPayServiceImpl sxfPayService;

    @Autowired
    private SftPayServiceImpl sftPayService;

    @Autowired
    private PayProperty payProperty;

    @Bean
    public PayMessageRouter payMessageRouter() {
        final PayMessageRouter wxOpenMessageRouter = new PayMessageRouter();
        //微信jsapi请求
        wxOpenMessageRouter.rule().async(false).payChannel(PayChannelEnum.WX.getType()).tradeType(WxPayEnum.JSAPI_MINI.getType()).handler(this.wxPayMiniHandler).end();
        wxOpenMessageRouter.rule().async(false).payChannel(PayChannelEnum.WX.getType()).tradeType(WxPayEnum.JSAPI_MP.getType()).handler(this.wxPayMiniHandler).end();

        /**
         * 微信h5
         */
        wxOpenMessageRouter.rule().async(false).payChannel(PayChannelEnum.WX.getType()).tradeType(WxPayEnum.H5.getType()).handler(this.wxPayMiniHandler).end();

        /**
         * ips
         */
        wxOpenMessageRouter.rule().async(false).payChannel(PayChannelEnum.IPS.getType()).tradeType(WxPayEnum.IPS_JS_API.getType()).handler(this.ipsPayService).end();

        /**
         * sxf
         */
        wxOpenMessageRouter.rule().async(false).payChannel(PayChannelEnum.SXF.getType()).tradeType(WxPayEnum.SXF_JS_API.getType()).handler(this.sxfPayService).end();

        /**
         * sft
         */
        wxOpenMessageRouter.rule().async(false).payChannel(PayChannelEnum.SFT.getType()).tradeType(WxPayEnum.SFT_JS_API.getType()).handler(this.sftPayService).end();

        return wxOpenMessageRouter;
    }

    @Bean
    public Snowflake outTradeNoSnowflake(){
        return IdUtil.getSnowflake(payProperty.getWorkerId(), payProperty.getDatacenterId());
    }
}
